package com.assessment.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.assessment.entity.Comprehensive;
import com.assessment.entity.Eliminate;
import com.assessment.entity.Newcome;
import com.assessment.entity.Staff;
import com.assessment.service.NewcomService;
import com.assessment.service.StaffService;

@Controller
@RequestMapping("/newCome")
public class NewcomeController {

	@Resource
	private NewcomService newcomService;
	
	@Resource
	private StaffService staffService;
	
	//获取新加人员信息
	@RequestMapping("/getNew.do")
	public String getNewComeInfo(Newcome newCome,Model model){
		model.addAttribute("newCome", newcomService.getNew());
		return "newCome/newComeInfo";
	}
	
	//跳到修改新加人员信息页面
	@RequestMapping(value="/updNew.do",produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String upd(Model model,@RequestParam("staffid")Integer id){
		System.out.println(id);
		model.addAttribute("newInfo",newcomService.selOne(id));
		Newcome com=newcomService.selOne(id);
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		com.setZt(format.format(new Date(com.getZhongtu().getTime())));
		
		JSONObject object=JSONObject.fromObject(com);
		return object.toString();
	}
	//修改新加人员信息
	@RequestMapping("/updNewCome.do")
	public String updNew(Model model,Newcome newCome,@RequestParam("staffid")Integer id){
		this.newcomService.updNewcome(newCome);
		model.addAttribute("newInfo",newcomService.selOne(id));
		return "redirect:/newCome/getNew.do";
	}
		
		//跳到新增新加人员信息页面
		@RequestMapping("/add.do")
		public String add(Newcome newcome){
			return "newCome/addNewCome";
		}
		//新增新加人员
		@RequestMapping("/addNewCome.do")
		public String addEliminate(Staff staff,@RequestParam("reason")String reasion,@RequestParam("jieduan")Integer jiedan){
			Newcome newcome=new Newcome();
			newcome.setName(staff.getStaffName());
			newcome.setStaffid(staff.getStaffId());
			newcome.setReason(reasion);
			newcome.setJieduan(jiedan);
			newcome.setZhongtu(staff.getZhongtu());
			//向员工表中插入信息
			System.out.println(staff.getStaffId());
			//this.staffService.insertStaff(staff);
			this.newcomService.addNew(newcome);
			//this.staffService.updState(eliminate.getStaffId());
			return "redirect:/newCome/getNew.do";
		}
	
	
	//删除人员信息
	@RequestMapping("delNew.do")
	public String delNew(Newcome newcome){
		this.newcomService.delNew(newcome);
		return "redirect:/newCome/getNew.do";
	}
}
